<template>
  <div class="dashboard-editor-container">
    <div>
      <el-card>
        <el-form :model="ruleForm" style="margin-top:10px;" size="mini" label-position="top" ref="ruleForm">
          <el-row :gutter="10">
            <el-col :lg="4" :md="6">
              <el-form-item label="渠道" prop='CHANNELTYPE'>
                <el-select v-model="ruleForm.CHANNELTYPE" filterable clearable placeholder="请选择业务渠道">
                  <el-option v-for="item in channelOption" :key="item.FIELDKEY" :label="item.FIELDVALUE"
                    :value="item.FIELDKEY"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="列数" prop='DBSEQ'>
                <el-input size="mini" placeholder="请输入列数" v-model="ruleForm.DBSEQ"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :md="12" class="searchBtnMarginTop" >
              <el-button type="primary" icon="el-icon-search" :loading="loading" size="mini" @click="queryImportData">查询
              </el-button>
              <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('ruleForm')">重置</el-button>
              <el-button type="success" size="mini" icon="el-icon-plus" @click="openEditDialog('','add')">新增字段
              </el-button>
            </el-col>
          </el-row>
        </el-form>
        <el-table :data="tableData"  border highlight-current-row class="tableMarginTop">
          <el-table-column prop="DBSEQ" align="center" label="列数"></el-table-column>
          <el-table-column prop="CHANNELTYPE" align="center" label="渠道" :formatter="dataFormat" width="100"></el-table-column>
          <el-table-column prop="DBCOL" align="center" label="数据库列" width="130"></el-table-column>
          <el-table-column prop="COLNAME" align="center" label="列名称" width="100"></el-table-column>
          <el-table-column prop="DATAFORMAT" align="center" label="数据格式"></el-table-column>
          <el-table-column prop="DATATYPE" align="center" label="数据类型" width="130" :formatter="dataFormat"></el-table-column>
          <el-table-column prop="DATALEN" align="center"  label="长度"></el-table-column>
          <el-table-column prop="DATAMUST" align="center" label="是否必输" :formatter="dataFormat"></el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="200">
            <template slot-scope="scope">
              <div>
                <el-button type="warning" size="mini" icon="el-icon-edit" @click="openEditDialog(scope.row,'update')">
                  修改</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="delImportData(scope.row)">删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination style="text-align: right; margin-top:20px;" :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize" @size-change="handleInSizeChange" @current-change="handleInCurrentChange"
              :current-page="currpage" background layout="sizes, total, prev, pager, next, jumper" :total="tableTotal">
            </el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <component :is="dialogList.editDialog" :option="editOptions" @refreshTable="queryImportData"></component>
  </div>
</template>

<script>
  // 发送接口
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import request from "@/common/utils/requestQC";
  import queyData from "@/message/QCManage/dataCollectionManage/importDataManage/importDataManageQuery";
  import delData from "@/message/QCManage/dataCollectionManage/importDataManage/importDataManageDel";
  import toolBox from "@/common/utils/toolBox";
  import editDialog from "./dialog/editDialog";
  import dictionary from '@/message/wfm/baseData/dictionaryManage/dictionaryQuery'
  import requestWFM from "@/common/utils/requestWFM";
  //批量上传
  export default {
    name: '',
    data() {
      return {
        inputOption:[],//是否必输
        dataTypeOption:[],//数据类型
        lengthOption:[],//长度
        DBCOLOption:[],//数据库列
        channelOption: [], //渠道类型
        isInputOption: {
          'Y': '必输',
          'N': '非必输'
        },
        dialogList: {
          editDialog
        },
        editOptions: {
          isShow: false,
          title: '',
          row: {},
          flag: ''
        },
        tableTotal: 1,
        currpage: 1,
        pageSize: getGlobalParams.get("PageSize"),
        tableData: [],
        ruleForm: {
          CHANNELTYPE: '', //渠道
          DBSEQ: '', //字段在文件中的列数
        },
        loading: false,
      }
    },
    computed: {},
    created() {

    },
    mounted() {
      this.queryImportData()
      this.dictionaryQuery('DBCOL').then(value => this.DBCOLOption = value)
      this.dictionaryQuery('CHANNELTYPE').then(value => this.channelOption = value)
      this.dictionaryQuery('DATALEN').then(value => this.lengthOption = value)
      this.dictionaryQuery('DATATYPE').then(value => this.dataTypeOption = value)
      this.dictionaryQuery('DATAMUST').then(value => this.inputOption = value)
    },
    watch: {},
    methods: {
       async dictionaryQuery(type) {
        let resBody = new dictionary();
        resBody.data.FIELDCODE = type
        resBody.data.CurrPage = 1
        resBody.data.PageRowNum = 400
        let response = await requestWFM(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          return response.BODY.FIEDATA;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // // 查询业务渠道
      // async channelQuery() {
      //   let resBody = new dictionary();
      //   resBody.data.FIELDCODE = 'CHANNELTYPE'
      //   resBody.data.CurrPage = 1
      //   resBody.data.PageRowNum = 100
      //   let response = await requestWFM(resBody);
      //   if (response.SYS_HEAD.ReturnCode == "000000") {
      //     this.channelOption = response.BODY.FIEDATA;
      //   } else {
      //     this.$message({
      //       type: "error",
      //       message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
      //     })
      //   }
      // },
      // // 查询数据库列
      // async DBCOLQuery() {
      //   let resBody = new dictionary();
      //   resBody.data.FIELDCODE = 'DBCOL'
      //   resBody.data.CurrPage = 1
      //   resBody.data.PageRowNum = 500
      //   let response = await requestWFM(resBody);
      //   if (response.SYS_HEAD.ReturnCode == "000000") {
      //     this.DBCOLOption = response.BODY.FIEDATA;
      //   } else {
      //     this.$message({
      //       type: "error",
      //       message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
      //     })
      //   }
      // },
      dataFormat(row, column) {
        //枚举设备形式
        if (column.property == "DATAMUST")
          return this.isInputOption[row[column.property]];
        if (column.property == "CHANNELTYPE")
          return this.findName(this.channelOption,row[column.property]);
        if (column.property == "DATATYPE")
          return this.findName(this.dataTypeOption,row[column.property]);
      },
      findName(arr,val){
        let data = arr.filter(item=>{
          return item.FIELDKEY == val
        })
        if(data.length)
        return data[0].FIELDKEY+' - '+data[0].FIELDVALUE
      },
      //打开修改新增Dialog
      async openEditDialog(row, flag) {
        console.log(row)
        this.editOptions.flag = flag
        this.editOptions.inputOption = this.inputOption
        this.editOptions.dataTypeOption = this.dataTypeOption
        this.editOptions.lengthOption = this.lengthOption
        this.editOptions.channelOption = this.channelOption
        this.editOptions.DBCOLOption = this.DBCOLOption
        if (flag == 'add') {
          this.editOptions.isShow = true
          this.editOptions.title = '新增采集数据导入字段'
        } else {
          this.editOptions.isShow = true
          this.editOptions.title = '修改采集数据导入字段'
          this.editOptions.row = row
        }
      },
      //前端分页
      handleInCurrentChange(val) {
        this.currpage = val;
        // console.log(`当前页: ${val}`);
        this.queryImportData()
      },
      handleInSizeChange(val) {
        this.pageSize = val;
        this.queryImportData()
      },
      //删除导入字段
      delImportData(row) {
        this.$confirm('此操作将删除该字段, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let resBody = new delData()
          resBody.data.DBCOL = row.DBCOL
          resBody.data.CHANNELTYPE = row.CHANNELTYPE
          let response = await request(resBody);
          if (response.SYS_HEAD.ReturnCode == "000000") {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.queryImportData()
          } else {
            this.$message({
              type: "error",
              message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 查询评分模板
      async queryImportData() {
        let resBody = new queyData()
        resBody.data.CHANNELTYPE = this.ruleForm.CHANNELTYPE
        resBody.data.DBSEQ = this.ruleForm.DBSEQ
        resBody.data.PageFlag = 4
        resBody.data.CurrPage = this.currpage;
        resBody.data.PageRowNum = this.pageSize;
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.BODY.RSPSTRUCT
          this.tableTotal = response.BODY.TotalRowNum
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 重置查询条件
      clearForm(item) {
        // 重置表格
        this.$refs["" + item].resetFields();
      },
    }
  }

</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  

</style>
